package by.averianov.discounta.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.averianov.discounta.dataaccess.PriceBookDao;
import by.averianov.discounta.datamodel.PriceBook;
import by.averianov.discounta.services.EconomistService;

@Service
public class EconomistServiceImpl implements EconomistService {

	private static final Logger LOGGER = LoggerFactory.getLogger(EconomistServiceImpl.class);
	
	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. 
		// Can be used for any initialization process.
		LOGGER.info("Instance of EconomistService is created. Class is: {}", getClass().getName());
	}

	@Inject
	protected PriceBookDao priceBookDao;
	
	@Override
	public List<PriceBook> getAllPriceBook(SingularAttribute<PriceBook, ?> attr, boolean ascending, int startRecord, int pageSize) {
		return priceBookDao.getAllPriceBook(attr, ascending, startRecord, pageSize);
	}
	
	@Override
	public PriceBook getPriceBookById(Long id) {
		return priceBookDao.getById(id);
	}
	
	@Override
	public void removePriceBook(Long id) {
		priceBookDao.delete(id);
	}
	
	@Override
	public void removeAllPriceBook() {
		priceBookDao.deleteAll();
	}
	
	@Override
	public void saveOrUpdatePriceBook(PriceBook priceBook) {
		if (priceBook.getId() == null) {
            LOGGER.debug("Save new: {}", priceBook);
            priceBookDao.insert(priceBook);
        } else {
            LOGGER.debug("Update: {}", priceBook);
            priceBookDao.update(priceBook);
        }
	}
	
	@Override
	public Long getPriceBookCount() {
		return priceBookDao.getCount();
	}

}
